package com.sgzn.sgpay.web.controller;

import com.sgzn.sgpay.enums.SgsResponseCode;
import com.sgzn.sgpay.model.request.SgsDeviceIncomePreInfoRequest;
import com.sgzn.sgpay.model.request.SgsDeviceIncomeRequest;
import com.sgzn.sgpay.model.request.util.PageQueryParam;
import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.model.response.result.SgsDeviceIncomePreInfoResult;
import com.sgzn.sgpay.model.response.result.SgsDeviceIncomeResult;
import com.sgzn.sgpay.service.SgsDeviceIncomePreInfoService;
import com.sgzn.sgpay.service.SgsDeviceIncomeService;
import com.sgzn.sgpay.util.ArgsUtils;
import com.sgzn.sgpay.web.controller.base.MerchantBaseController;
import com.sgzn.sgpay.web.handle.AssembleCommonHandle;
import com.sgzn.sgpay.web.request.Impl.DeviceIncomePreInfoRequest;
import com.sgzn.sgpay.web.request.Impl.DeviceIncomeRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

/**
 * 经营分析前查询展示信息控制器
 * @author huangsenguang
 * @version Id: DeviceStatisticsController.java, v 0.1 2018/5/27 16:55 huangsenguang Exp $$
 */
@Slf4j
@Controller
@RequestMapping("/merchant/statistics")
public class DeviceStatisticsController extends MerchantBaseController {

    @Autowired
    private SgsDeviceIncomePreInfoService sgsDeviceIncomePreInfoService;

    @Autowired
    private SgsDeviceIncomeService sgsDeviceIncomeService;

    @Resource
    private AssembleCommonHandle assembleCommonHandle;

    /**
     * 设备收益前查询展示信息
     * @param request
     * @return
     */
    @RequestMapping("deviceIncomePreInfo")
    @ResponseBody
    public SgsResultResponse<SgsDeviceIncomePreInfoResult> queryIncomePreInfo(DeviceIncomePreInfoRequest request) {
        super.setMchInfo(request);
        return this.sgsDeviceIncomePreInfoService.queryInfo(assembleRequest(request));
    }

    /**
     * 设备经营分析
     * @param request
     * @return
     */
    @RequestMapping("deviceIncome")
    @ResponseBody
    public SgsResultResponse<SgsDeviceIncomeResult> queryIncome(DeviceIncomeRequest request) throws ParseException {
        super.setMchInfo(request);
        return this.sgsDeviceIncomeService.queryInfo(assembleRequest(request));
    }

    /**
     * 组装服务请求
     * @param request
     * @return
     */
    private SgsDeviceIncomePreInfoRequest assembleRequest(DeviceIncomePreInfoRequest request) {
        SgsDeviceIncomePreInfoRequest sgsDeviceIncomePreInfoRequest = new SgsDeviceIncomePreInfoRequest();
        this.assembleCommonHandle.assemble(request, sgsDeviceIncomePreInfoRequest);
        return sgsDeviceIncomePreInfoRequest;
    }

    /**
     * 组装服务请求
     * @param request
     * @return
     */
    private SgsDeviceIncomeRequest assembleRequest(DeviceIncomeRequest request) throws ParseException {
        SgsDeviceIncomeRequest sgsDeviceIncomeRequest = new SgsDeviceIncomeRequest();
        this.assembleCommonHandle.assemble(request, sgsDeviceIncomeRequest);
        if (StringUtils.isNotBlank(request.getDeviceTypeList())) {
            String[] deviceTypeList = request.getDeviceTypeList().split(",");
            sgsDeviceIncomeRequest.setDeviceTypeList(Arrays.asList(deviceTypeList));
        }
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        sgsDeviceIncomeRequest.setBeginTime(simpleDateFormat.parse(request.getBeginTime()));
        if (StringUtils.isNotBlank(request.getEndTime())){
            Date endTime = simpleDateFormat.parse(request.getEndTime());
            endTime.setTime(endTime.getTime()+ 86399999);
            sgsDeviceIncomeRequest.setEndTime(endTime);
        } else {
            sgsDeviceIncomeRequest .setEndTime(new Date());
        }
        ArgsUtils.check(sgsDeviceIncomeRequest.getBeginTime().before(sgsDeviceIncomeRequest.getEndTime()),
                SgsResponseCode.C10018.getResponseCode(),
                SgsResponseCode.C10018.getResponseDesc());
        PageQueryParam pageQueryParam = new PageQueryParam();
        pageQueryParam.setPageSize(Integer.parseInt(request.getPageSize()));
        pageQueryParam.setCurrentPage(Integer.valueOf(request.getPageNum()));
        sgsDeviceIncomeRequest.setPageQueryParam(pageQueryParam);
        return sgsDeviceIncomeRequest;
    }
}

